from wormbane.Thing import Thing
import random


class Feature(Thing):

    passable = True
    color = 'black'

    @property
    def diagonally_passable(self):
        return self.passable

    def search(self):
        return None


class Wall(Feature):

    passable = False

    def __init__(self, dir=None):
        Feature.__init__(self)
        self.dir = dir
        self.secret = False

    @property
    def char(self):
        if self.dir:
            return '|' if self.dir in ('west', 'east') else '-'
        else:
            return ' '

    def search(self):
        if self.secret:
            if self.dir:
                door = Door(dir=self.dir)
                door.closed = True
                door.locked = bool(random.randrange(2))
                self.env.feature = door
                return door
            else:
                corridor = Corridor()
                self.env.feature = corridor
                return corridor
        else:
            return None


class Floor(Feature):
    char = '.'


class Corridor(Feature):
    char = '#'


class Door(Feature):

    diagonally_passable = False
    color = 'yellow'

    def __init__(self, dir):
        Feature.__init__(self)
        self.dir = dir
        self.closed = True
        self.locked = False

    @property
    def char(self):
        if self.closed:
            return '+'
        else:
            return '|' if self.dir in ('north', 'south') else '-'

    @property
    def passable(self):
        return not self.closed


class Doorway(Feature):

    char = '.'
    color = 'yellow'

    def __init__(self, dir):
        Feature.__init__(self)
        self.dir = dir


class Stair(Feature):

    def __init__(self, dir):
        Feature.__init__(self)
        self.dir = dir

    @property
    def char(self):
        return '<' if self.dir == 'up' else '>'
